The following is a synopsis of functions available in the standard ANSI C library. Other on-line reference tools (i.e., Symantec THINK Reference) provide a more detailed review of the ANSI C Library functions, but a quick review of these functions is provided in this section for your convenience.
As a note, if you plan on programming for the MacOS, you'll find that you won't be using the ANSI C library as much as you might think. This goes especially for input and output functions. This means that you probably don't need to spend a lot of time learning the ins and outs of the ANSI C library - although you should at least be aware of the capabilities it provides.
int fprintf(FILE *stream, const char *format, ...)
Returns number of characters printed, or negative result if error. Conversion specification begins with '%' and ends with conversion character. In between, there may be (in order):
flags:
'-' left justify
'+' print with sign
' ' <space> prefix with space if no sign
'0' <zero> specifies padding with leading zeros
'#' alternate output form
numbers:
n first number specifies field width
'.' period used as separator
n second number specifies precision (max characters)
m modifier (h - short, l - long, L - long double)
format characters:
d,i int; signed
o int; unsigned octal (without leading zero)
x,X int; unsigned hexadecimal
u int; unsigned
c int; single character
s char *; string
f double;
e,E double; scientific notation
g,G double; %e or %f format, depending on value
p void *; pointer
n int *; number of characters written so far
% no argument, print '%'
example:
fprintf( myFilename, "The results are %d and %6.2f\n", myInt, myReal);
Reads from 'stream' and assigns values through subsequent arguments which must be pointers. Returns EOF if end of file occurs before any conversion; otherwise it returns number of items assigned. Format string should contain conversion specifications consisting of '%' followed by:
flag:
* suppression; ignore field
number:
n first number specifies maximum field width
width character:
h short
l long
L long double
conversion characters:
d int *
i int *; may be octal (leading 0) or hexadecimal (leading 0x)
o int *; octal integer
x int *; hexadecimal integer
c char *; characters
s char *; string of non-white characters
e,f float *; could also use 'g'
p void *; pointer
n int *; writes into arg number of characters read. Nothing read.
FSEEK
int fseek(FILE *stream, long offset, int origin)
Sets file position. Values for origin can be:
SEEK_SET beginning
SEEK_CUR current position
SEEK_END end of file
FSETPOS
int fsetpos(FILE *stream, const fpos_t *ptr)
Positions 'stream' at position recorded by fgetpos. Returns non-zero if error.
FTELL
long ftell(FILE *stream)
Returns current position for 'stream', or -1L if error.
Use the functions in <stdarg.h> to retrieve arguments from a variable length function argument list. An example of using variable argument lists can be found in Section 'Functions'. Before using functions, you must declare a variable of type 'va_list' as follows:
va_list ap;
You then use the 'va_start' macro to initialize. Afterwords, each subsequent call to 'va_arg' returns the next argument. Finally, call 'va_end'.
VA_START
void va_start(va_list ap, lastarg)
Provide the last argument in a variable length function list as 'lastarg' to initialize va_list variable.
VA_ARG
type va_arg(va_list ap, type)
Returns next argument in variable length function list. You must supply the data type in 'type'. va_arg returns type specified in 'type'.
VA_END
void va_end(va_list ap)
Execute after arguments have been processed, but prior to exiting function.
Bad idea which is rarely used but included just to be complete.
SETJMP
int setjmp(jmp_buf env)
Saves state information for later use by 'longjmp'.
LONGJMP
void longjmp( jmp_buf env, int val)
Restores state saved by most recent call to setjmp. Execution resumes as if the setjmp function had just executed and returned the non-zero value val. Function containing setjmp must not have terminated.